﻿Imports System.Data.OleDb

Public Class SiteUserGateway
    Inherits DataGateway

    Protected Overrides ReadOnly Property FindAllQuery As String
        Get
            Return "SELECT * FROM SiteUser"
        End Get
    End Property

    Protected Overrides ReadOnly Property DeleteQuery As String
        Get
            Return "DELETE FROM SiteUser"
        End Get
    End Property

    Protected Overrides ReadOnly Property SiteUserQuery() As String
        Get
            Return "SELECT * FROM SiteUser WHERE ID=@ID"
        End Get
    End Property

    Public Overloads Function Exists(ByVal userName As String) As Boolean
        Return FindByUserName(userName).Rows.Count() = 1
    End Function

    Public Function FindByUserName(ByVal userName As String) As DataTable
        Dim sqlQuery As String = "SELECT * FROM SiteUser WHERE UserName=@UserName"
        Dim queryParams As New OrderedDictionary()
        queryParams.Add("@UserName", userName)
        Return QueryTable(sqlQuery, queryParams)
    End Function

    Public Function Insert(ByVal userName As String, Optional ByVal firstName As String = "", Optional ByVal lastName As String = "", Optional ByVal photo As String = "person.png") As ULong
        Dim newID As ULong
        Dim sqlQuery As String = "INSERT INTO SiteUser (UserName, FirstName, LastName, Photo) VALUES (@UserName, @FirstName, @LastName, @Photo)"
        Using connection As New OleDbConnection(ConnectionString)
            Using command As New OleDbCommand(sqlQuery, connection)
                command.CommandType = CommandType.Text
                command.Parameters.AddWithValue("@UserName", userName)
                command.Parameters.AddWithValue("@FirstName", firstName)
                command.Parameters.AddWithValue("@LastName", lastName)
                command.Parameters.AddWithValue("@Photo", photo)
                connection.Open()
                command.ExecuteNonQuery()
                newID = GetLastInsertID(command)
            End Using
        End Using

        Return newID
    End Function

    Public Sub Update(ByVal userName As String, ByVal firstName As String, ByVal lastName As String, Optional ByVal photo As String = Nothing)
        Dim sqlQuery As String
        Dim where As String = " WHERE UserName=@UserName"
        If photo Is Nothing Then
            sqlQuery = "UPDATE SiteUser SET FirstName=@FirstName, LastName=@LastName" & where
        Else
            sqlQuery = "UPDATE SiteUser SET FirstName=@FirstName, LastName=@LastName, Photo=@Photo" & where
        End If
        Using connection As New OleDbConnection(ConnectionString)
            Using command As New OleDbCommand(sqlQuery, connection)
                command.CommandType = CommandType.Text
                command.Parameters.AddWithValue("@FirstName", firstName)
                command.Parameters.AddWithValue("@LastName", lastName)
                If photo IsNot Nothing Then
                    command.Parameters.AddWithValue("@Photo", photo)
                End If
                command.Parameters.AddWithValue("@UserName", userName)
                connection.Open()
                command.ExecuteNonQuery()
            End Using
        End Using
    End Sub
End Class